package com.hwhhhh.mygrad.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.interfaces.ClsSBListener;

public class SchoolBuildingAdapter extends RecyclerView.Adapter<SchoolBuildingAdapter.SchoolBuildingHolder> {
    private static final String TAG = "SchoolBuilding";
    private String[] sbs;
    private int selected = 0;
    private TextView tvSelected;
    private Context mContext;
    private ClsSBListener clsSBListener;

    public SchoolBuildingAdapter(Context context, String[] sbs) {
        this.mContext = context;
        this.sbs = sbs;
    }

    public String[] getSbs() {
        return sbs;
    }

    public void setSbs(String[] sbs) {
        this.sbs = sbs;
    }

    @NonNull
    @Override
    public SchoolBuildingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_school_building, parent, false);
        return new SchoolBuildingHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull SchoolBuildingHolder holder, int position) {
        holder.tvSchoolBuilding.setText(sbs[position]);
        if (selected == position) {
            tvSelected = holder.tvSchoolBuilding;
            tvSelected.setBackgroundResource(R.drawable.bg_school_building_selected);
            tvSelected.setTextColor(Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(mContext,R.color.white))));
        } else {
            holder.tvSchoolBuilding.setBackgroundResource(R.drawable.bg_school_building_unselected);
            holder.tvSchoolBuilding.setTextColor(Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(mContext,R.color.white))));
        }
        holder.tvSchoolBuilding.setOnClickListener(v -> {
            doClick(v, position);
            if (clsSBListener != null) clsSBListener.onChangeCls(selected);
        });
    }

    @Override
    public int getItemCount() {
        return sbs.length;
    }

    @Override
    public int getItemViewType(int position) {
        Log.d(TAG, "getItemViewType: position" + position);
        return super.getItemViewType(position);
    }

    public static class SchoolBuildingHolder extends RecyclerView.ViewHolder {
        public TextView tvSchoolBuilding;

        public SchoolBuildingHolder(@NonNull View itemView) {
            super(itemView);
            tvSchoolBuilding = itemView.findViewById(R.id.tv_school_building);
        }
    }

    public void setClsSBListener(ClsSBListener listener) {
        this.clsSBListener = listener;
    }

    public void doClick(View v, int position) {
        if (selected != position) {
            selected = position;
            tvSelected.setBackgroundResource(R.drawable.bg_school_building_unselected);
            tvSelected.setTextColor(Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(mContext, R.color.white))));
            tvSelected = (TextView) v;
            tvSelected.setBackgroundResource(R.drawable.bg_school_building_selected);
            tvSelected.setTextColor(Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(mContext, R.color.white))));
        }
    }
}
